//implement GTplTestRunner
//andycai_sc 2011-05-15

#include "GTplTester.h"

GTplTestRunner::GTplTestRunner(int argc, char** argv) : m_isAll(false)
{
	for (int i = 0; i < argc; ++i)
		m_args.push_back(argv[i]);
}

GTplTestRunner::~GTplTestRunner()
{
}

bool GTplTestRunner::parseArgs()
{
	if (m_args.size() < 2)
	{
		help();
		return false;
	}

	if (m_args[1].compare("-all") == 0)
		m_isAll = true;
	else if (m_args[1].compare("-?") == 0 || m_args[1].compare("-help") == 0)
	{
		help();
		return false;
	}
	else
	{
		for (int i = 1; i < m_args.size(); ++i)
		{
			map<string, GTplTester*>::iterator it;
			if ((it = m_testers.find(m_args[i])) != m_testers.end())
				m_runalbeTesters.push_back(it->second);
			else
				m_unknownTests.push_back(it->first);
		}
	}

	return true;
}

void GTplTestRunner::addTester(GTplTester* tester)
{
	m_testers.insert(map<string, GTplTester*>::value_type(tester->getTestName(), tester));
}

void GTplTestRunner::runTest()
{
	if (m_isAll)
	{
		map<string, GTplTester*>::iterator it = m_testers.begin();
		for (; it != m_testers.end(); ++it)
			run(it->second);
	}
	else
	{
		vector<GTplTester*>::iterator it;
		for (it = m_runalbeTesters.begin(); it != m_runalbeTesters.end(); ++it)
			run(*it);
	}

	if (m_unknownTests.size() != 0)
	{
		cout << "Test finished.but there don't have these testcases:";
		for (vector<string>::iterator it = m_unknownTests.begin();
			it != m_unknownTests.end(); ++it)
			cout << *it;
		cout << endl;
	}
}

void GTplTestRunner::help()
{
	cout << "Usage:" << m_args[0] << " -all | -? | -help | testname1 testname2 testname3 ..... " << endl;
}

void GTplTestRunner::run(GTplTester* tester)
{
	cout << ">>>>>>>>>>>>>>>>>>>>>>>>" << tester->getTestName();
	cout << " begin>>>>>>>>>>>>>>>>>>>>>>>" << endl << endl;
	tester->run();
	cout << "<<<<<<<<<<<<<<<<<<<<<" << tester->getTestName();
	cout << " end<<<<<<<<<<<<<<<<<<<<<<<<" << endl << endl;
}
